package com.example.genturedemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity3 extends AppCompatActivity implements GestureDetector.OnGestureListener {

    private GestureDetector detector;
    private ImageView imageView;

    private Bitmap bitmap;

    private int width, height;

    //当前的缩放比例
    private float currentScale = 1.0f;

    //控制图片缩放的对象
    private Matrix matrix;

    private static final String TAG = "mainTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

        detector = new GestureDetector(this);
        imageView = findViewById(R.id.show);

        matrix = new Matrix();
        bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.test));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    //触摸事件按下时触发
    @Override
    public boolean onDown(@NonNull MotionEvent e) {
        Log.e(TAG, "onDown: " );
        Toast.makeText(this,"onDown",Toast.LENGTH_SHORT).show();
        return false;
    }

    //用户手指触摸屏幕按下,但是未移动或松开时触发
    @Override
    public void onShowPress(@NonNull MotionEvent e) {
        Log.e(TAG, "onShowPress: " );
        Toast.makeText(this,"onShowPress",Toast.LENGTH_SHORT).show();
    }

    //用户轻点屏幕时触发
    @Override
    public boolean onSingleTapUp(@NonNull MotionEvent e) {
        Log.e(TAG, "onSingleTapUp: " );
        Toast.makeText(this,"onSingleTapUp",Toast.LENGTH_SHORT).show();
        return false;
    }

    //在屏幕上滑动触发
    @Override
    public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
        Log.e(TAG, "onScroll: " );
        Toast.makeText(this,"onScroll",Toast.LENGTH_SHORT).show();
        return false;
    }

    //在屏幕长按触发
    @Override
    public void onLongPress(@NonNull MotionEvent e) {
        Log.e(TAG, "onLongPress: " );
        Toast.makeText(this,"onLongPress",Toast.LENGTH_SHORT).show();
    }

    //用户在屏幕滑动结束后触发
    @Override
    public boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {

        //通过左右滑动来缩放图片
        extracted(velocityX);
        return true;
    }

    private void extracted(float velocityX) {
        velocityX = velocityX > 3000 ? 3000 : velocityX;
        velocityX = velocityX < -3000 ? -3000 : velocityX;

        currentScale += currentScale * velocityX / 3000.0f;
        // 让缩放比不等于 0
        currentScale = currentScale > 0.01 ? currentScale: 0.01f;
        // 重置 matrix （如果不重置，且手通过屏幕缩放的次数过多，可能会导致内存溢出 OutOfMemoryError）
        matrix.reset();
        // 缩放
        matrix.setScale(currentScale, currentScale, 150, 200);
        BitmapDrawable bitmapDrawable = (BitmapDrawable)imageView.getDrawable();

        // 强制回收没有回收的图片
        if (!bitmapDrawable.getBitmap().isRecycled())
            bitmapDrawable.getBitmap().recycle();

        // 回收后，根据原始图和 matrix 创建新的图片
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        // 显示新的图片
        imageView.setImageBitmap(bitmap2);
    }
}